<?php

/**
 * Chart class
 *
 * General chart properties,
 * Chart data arrays
 * Common chart functions
 *
 * @name Chart
 * @author DiMoN
 * @copyright Dmitry Pavlov
 */

class Chart
{
    /**
     * Chart settings
     */
    protected static $Width               ;
    protected static $Height              ;
    protected static $Depth               ;
    protected static $ColorTitle          ;
    protected static $BackgroundTitle1    ;
    protected static $BackgroundTitle2    ;
    protected static $BackgroundArea1     ;
    protected static $BackgroundArea2     ;
    protected static $HorizontalLineColor ;
    protected static $VerticalLineColor   ;
    protected static $BorderColor         ;
    protected static $Background          ;
    protected static $TitleFontSize       ;
    protected static $TitleFont           ;
    protected static $LabelFontAngle      ;
    protected static $LabelLevel          ;
    protected static $LabelFont           ;

    /**
     * Data arrays
     */
    protected static $Data2D              ;
    protected static $Data3D              ;

    /**
     * Labels array
     */
    protected static $Labels              ;
    protected static $LeveledLabels       ;

    protected static $Legend              ;

    protected static $Title               ;
    protected static $ParamName           ;
    protected static $NameX               ;
    protected static $NameY               ;

    /**
     * Chart constructor
     * Sets Chart settings, data arrays and titles
     *
     * @param array $data2D - Data array for Charts which support only one dataset (type 2)
     * @param array $data3D - Data array for Charts which support multiple datasets (type3)
     * @param array $labels - Labels array
     * @param array $legend - Legend array
     * @param string $title - Chart title
     * @param string $param_name - Parameter name
     * @param string $name_x - Axis X name
     * @param string $name_y - Axis Y name
     */
    function __construct($data2D, $data3D, $labels, $legend, $title, $param_name, $name_x, $name_y)
    {
        require_once("lib/phpchartdir.php");

        $this->setSettings();
        $this->setData($data2D, $data3D, $labels, $legend);
        $this->setTitles($title, $param_name, $name_x, $name_y);
    }

    /**
     * Sets Chart settings
     *
     */
    private function setSettings()
    {
        self::$Width               = 900              ;
        self::$Height              = 600              ;
        self::$Depth               = 10               ;
        self::$ColorTitle          = 0x001f1f2f       ;
        self::$BackgroundTitle1    = 0x00cfcfcf       ;
        self::$BackgroundTitle2    = 0x00000000       ;
        self::$BackgroundArea1     = 0x00ffddff       ;
        self::$BackgroundArea2     = 0x00ccaacc       ;
        self::$HorizontalLineColor = 0x0000ffff       ;
        self::$VerticalLineColor   = 0x00ffccff       ;
        self::$BorderColor         = 0x00ffff00       ;
        self::$Background          = 0x00efffdf       ;
        self::$TitleFontSize       = 12               ;
        self::$TitleFont           = 'arial.ttf Bold' ;
        self::$LabelFontAngle      = 0                ;
        self::$LabelLevel          = 2                ;
        self::$LabelFont           = 'arial.ttf Bold' ;
    }

    /**
     * Sets Chart titles
     *
     * @param string $title - Chart title
     * @param string $param_name - Parameter name
     * @param string $name_x - Axis X name
     * @param string $name_y - Axis Y name
     */
    private function setTitles($title = null, $param_name, $name_x, $name_y)
    {
        self::$ParamName = $param_name ;
        self::$NameX     = $name_x     ;
        self::$NameY     = $name_y     ;

        if (!empty($title))
        {
            self::$Title = $title;
        }
        else
        {
            self::$Title = implode(' - ', array_filter(array(self::$ParamName, self::$NameX, self::$NameY), array($this, "notEmpty")));
        }
    }

    /**
     * Defines whether the variable is not empty
     *
     * @param mixed $var - Variable to check
     * @return bool
     */
    private function notEmpty($var)
    {
        return !empty($var);
    }

    /**
     * Sets Chart Data, Labels, Legend arrays
     *
     * @param array $data2D - Data array for Charts which support only one dataset (type 2)
     * @param array $data3D - Data array for Charts which support multiple datasets (type3)
     * @param array $labels - Labels array
     * @param array $legend - Legend array
     */
    private function setData($data2D, $data3D, $labels, $legend)
    {
        self::$Data2D = $data2D;
        self::$Data3D = $data3D;
        self::$Labels = $labels;
        self::$LeveledLabels = $this->setLabelsLevel($labels);
        self::$Legend = $legend;
    }

    /**
     * Gets random color
     *
     * @return int
     */
    protected function getColor()
    {
        return 1 * rand(0, 500000000000);
    }

    /**
     * Saves specified chart
     *
     * @param object $chart - Chart object
     * @return string
     */
    public function saveChart(&$chart)
    {
        $file_name  = Settings::$DiagramsDirectory . md5(uniqid(rand())) . Settings::CHART_FORMAT;
        $file_chart = fopen($file_name, "w");
        fwrite($file_chart, $chart->makeChart2(PNG));
        fclose($file_chart);
        return $file_name;
    }

    /**
     * Set each item of the labels array it's level
     * (to display labels without overlapping)
     *
     * @param array $labels - Labels array
     * @return array
     */
    protected function setLabelsLevel($labels)
    {
        $level = 1;
        foreach ($labels as $key => $value)
        {
            $labelValue = $value;
            for ($j = 1; $j < $level; $j++)
            {
                $labelValue = "\n" . $labelValue;
            }
            if ($level < self::$LabelLevel)
            {
                ++$level;
            }
            else
            {
                $level = 1;
            }
            $labels[$key] = $labelValue;
        }

        return $labels;
    }
}

?>
